Home > Java Programming > Inner Classes > Questions and Answers
01. |
What will be the output of the program? public class HorseTest { public static void main (String [] args) { class Horse { public String name; /* Line 7 */ public Horse(String s) { name = s; } } /* class Horse ends */ Object obj = new Horse("Zippo"); /* Line 13 */ Horse h = (Horse) obj; /* Line 14 */ System.out.println(h.name); } } /* class HorseTest ends */ | |||||||||||
|
02. |
What will be the output of the program? public abstract class AbstractTest
{ public int getNum() { return 45; } public abstract class Bar { public int getNum() { return 38; } } public static void main (String [] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; AbstractTest.Bar f = t.new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); } } | |||||||||||
|
03. | Which is true about an anonymous inner class? | |||||||||||
|
04. |
class Foo { class Bar{ } } class Test { public static void main (String [] args) { Foo f = new Foo(); /* Line 10: Missing statement ? */ } } which statement, inserted at line 10, creates an instance of Bar? | |||||||||||
|
05. |
Given below the sample code : class SuperClass { SuperClass() { } static class SubClass { SubClass() { } } } Which is the correct way to declare the object of "Subclass" , which can be use outside the "Superclass" ? | |||||||||||
|
06. |
Given below the sample code : class SuperClass { SuperClass() { } public class SubClass { SubClass() { } } } Which is the correct way to declare the object of public "Subclass" , which can be use outside the "Superclass" ? | |||||||||||
|
07. |
Given below the sample code : class Demo{ demo() { } public class Demo { funtion() { } } } How can we correct the above code? Select appropriate option : | |||||||||||
|
08. |
Given below the sample code : class SuperClass { public static void main(String[] args) { new SubClass(); } SuperClass() { System.out.print("Inside SuperClass "); } } class SubClass extends SuperClass { SubClass() { System.out.print("Inside SubClass"); } } What is the output of the following code ? | |||||||||||
|
09. |
What is the output for the below code ? public class Outer { private int a = 7; class Inner { public void displayValue() { System.out.println("Value of a is " + a); } } } public class Test { public static void main(String... args) throws Exception { Outer mo = new Outer(); Outer.Inner inner = mo.new Inner(); inner.displayValue(); } } | |||||||||||
|